package cn.glenn._04weibo.controller;

import cn.glenn._04weibo.base.response.JsonResult;
import cn.glenn._04weibo.mapper.CommentMapper;
import cn.glenn._04weibo.pojo.dto.CommentAddParam;
import cn.glenn._04weibo.pojo.enity.Comment;
import cn.glenn._04weibo.pojo.vo.UserVO;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@Slf4j
@RestController
@RequestMapping("v1/comment/")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;

    @PostMapping("insert")
    public JsonResult insert(@RequestBody CommentAddParam commentAddParam, HttpSession session) {
        if ((session.getAttribute("user"))==null) {return JsonResult.notLogin();}
        UserVO userVO = (UserVO) session.getAttribute("user");
        log.debug("userVO:{}",userVO);
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentAddParam, comment);
        comment.setUserId((long)userVO.getId());
        comment.setCreateTime(new Date());
        int result = commentMapper.insertComment(comment);
        log.debug("result:{}",result);
        return JsonResult.success();
    }


}
